﻿using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace LegendHub.Crypt
{
    public class Md5
    {
        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="strPwd">需要加密的内容</param>
        /// <returns>加密过的内容</returns>
        public static string Md5String(string strPwd)
        {
            MD5 mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            var bytes = Encoding.Default.GetBytes(strPwd);
            var numArray = mD5CryptoServiceProvider.ComputeHash(bytes);
            mD5CryptoServiceProvider.Clear();
            return numArray.Aggregate("", (current, t) => string.Concat(current, t.ToString("x").PadLeft(2, '0')));
        }

        /// <summary>
        /// MD5加密Byte数组
        /// </summary>
        /// <param name="b1">数组1</param>
        /// <param name="b2">数组2</param>
        /// <returns>加密过的Btye数组</returns>
        public static byte[] Md5Byte16(byte[] b1, byte[] b2 = null)
        {
            MD5 mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            var nums = new List<byte>();
            nums.AddRange(b1);
            if (b2 != null)
            {
                nums.AddRange(b2);
            }
            return mD5CryptoServiceProvider.ComputeHash(nums.ToArray());
        }
    }
}
